// 请求地址的基本路径
const baseUrl = 'https://x.bilinmeiju.com/app/'
const domain = 'https://x.bilinmeiju.com'
import store from '../store/index.js'
/**
 * @param {Object} path 跳转路径
 * @param {Object} type 跳转类型
 * @param {Object} nologin 无需登录
 */
function gopage(path, type, nologin) {
	if (!path) {
		uni.showToast({
			title: '开发中，敬请期待',
			icon: 'none'
		})
		return false;
	}

	let token = uni.getStorageSync('token');
	let shequ = uni.getStorageSync('shequ');

	if (!token && !nologin) {
		if (!shequ) {
			uni.navigateTo({
				url: '/pages/shequgeli/xiaooquxuanze'
			})
		} else {
			uni.navigateTo({
				url: '/pages/login/login'
			})
		}
		return false;
	}
	var type = type ? type : 1;
	switch (type) {
		case 1:
			uni.navigateTo({
				url: path
			})
			break;
		case 2:
			uni.redirectTo({
				url: path
			})
			break;
		default:
			uni.switchTab({
				url: path
			})
			break;
	}
}


function request(url, data, show = true) {
	data['communityId'] = uni.getStorageSync('communityId');

	// 显示加载数据的提示
	// uni.showLoading({
	// 		title: '',
	// 		mask: true
	// })

	return new Promise(function(resolve, reject) {
		uni.request({
			method: 'POST',
			url: baseUrl + url,
			header: {
				token: uni.getStorageSync('token')
			},
			data: data,
			sslVerify: false,
			success(res) {
				if (res.data.code == 10086) {

					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages/login/login"
						})
					}, 1500)
					return false
				}
				if (res.data.code === 402) {
					uni.setStorageSync('token', '');
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages/login/login"
						})
					}, 1500)
					return false
				}
				if (res.data.code > 0) {
					store.commit('hideLoading')
					var length = res.data.msg.length > 7 ? 'none' : 'error';
					uni.showToast({
						icon: length,
						title: res.data.msg
					})
					// reject(res.data)
					return false
				}
				resolve(res.data)
				store.commit('hideLoading')
			},
			fail(err) {
				store.commit('hideLoading')
				reject(err)
			},
			complete() {

			}
		})
	})
}

export default {
	request,
	domain,
	gopage
}
